//
//  XHFirstTypeTwoCell.m
//  umibus
//
//  Created by fengyn on 16/6/18.
//  Copyright © 2016年 lexingsoft. All rights reserved.
//

#import "XHFirstTypeTwoCell.h"

@implementation XHFirstTypeTwoView
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self commonInit];
    }
    return self;
}

- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder{
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self commonInit];
    }
    return self;
}

- (void)commonInit{
    UIView *borderView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 75, 70)];
    [self addSubview:borderView];
    self.borderView=borderView;
    
    borderView.layer.masksToBounds=YES;
    borderView.layer.cornerRadius=5;
    borderView.layer.borderColor=[ColorManager colorWithMain].CGColor;
    borderView.layer.borderWidth=1;
    
    UIButton *button=[[UIButton alloc] init];
    [button setBackgroundColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button setBackgroundColor:[ColorManager colorWithMain] forState:UIControlStateSelected];
    [button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
    [borderView addSubview:button];
    self.button=button;
    UILabel *label1=[[UILabel alloc] init];
    label1.font=[UIFont boldSystemFontOfSize:19];
    label1.textColor=[ColorManager colorWithMain];
    label1.textAlignment=NSTextAlignmentCenter;
    [borderView addSubview:label1];
    self.titleLabel=label1;
    UILabel *label2=[[UILabel alloc] init];
    label2.font=[UIFont systemFontOfSize:12];
    label2.textColor=[ColorManager colorWithMain];
    label2.textAlignment=NSTextAlignmentCenter;
    [borderView addSubview:label2];
    self.detailLabel=label2;
    UIImageView *imageView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"shouye_icon_cuxiao"]];
    [borderView addSubview:imageView];
    self.tagImageView=imageView;
    
    UILabel *noteLabel=[[UILabel alloc] init];
    noteLabel.font=[UIFont systemFontOfSize:11];
    noteLabel.textColor=[UIColor colorWithWhite:0 alpha:0.5];
    noteLabel.textAlignment=NSTextAlignmentCenter;
    [self addSubview:noteLabel];
    self.noteLabel=noteLabel;
}

- (void)buttonAction{
    [self setSelected:![self isSelected]];
    if ([self.delegate respondsToSelector:@selector(buttonViewDidSelectedAction:)]) {
        [self.delegate buttonViewDidSelectedAction:self];
    }
}

- (void)setSelected:(BOOL)selected{
    self.button.selected=selected;
    if (selected) {
        self.titleLabel.textColor=[UIColor whiteColor];
        self.detailLabel.textColor=[UIColor whiteColor];
    }else{
        self.titleLabel.textColor=[ColorManager colorWithMain];
        self.detailLabel.textColor=[ColorManager colorWithMain];
    }
}

- (BOOL)isSelected{
    return self.button.isSelected;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    [self updateRects];
}

- (void)setFrame:(CGRect)frame {
    [super setFrame:frame];
    
    [self updateRects];
}

- (void)updateRects {
    self.borderView.frame=CGRectMake(0, 0, self.frame.size.width, 70);
    self.button.frame=CGRectMake(0, 0, self.frame.size.width, 70);
    self.titleLabel.frame=CGRectMake(0, 17, self.frame.size.width, 23);
    self.detailLabel.frame=CGRectMake(0, 45, self.frame.size.width, 15);
    CGSize imageSize=self.tagImageView.image.size;
    self.tagImageView.frame=CGRectMake(self.frame.size.width-imageSize.width, 0, imageSize.width, imageSize.height);
    self.noteLabel.frame=CGRectMake(0, 70+8, self.frame.size.width, 12);
}


@end

@implementation XHFirstTypeTwoCell

- (void)awakeFromNib {
    [super awakeFromNib];
}

- (void)setdata:(NSArray *)productArray{
    self.m_dataArray=productArray;
    for (UIView *view in self.ui_borderView.subviews) {
        [view removeFromSuperview];
    }
    for(int i=0; i<productArray.count; i++){
        int row=i/4;
        int column=i%4;
        int screenWidth=[UIScreen mainScreen].bounds.size.width;
        int viewWidth=(screenWidth-40-10*(4-1))/4;
        XHFirstTypeTwoView *buttonView=[[XHFirstTypeTwoView alloc] initWithFrame:CGRectMake(column*viewWidth+column*10, row*95+row*10, viewWidth, 95)];
        buttonView.tag=i;
        buttonView.delegate=self;
        NSDictionary * dic=productArray[i];
        
        NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
        formatter.numberStyle = kCFNumberFormatterRoundFloor;
        NSString *string = [formatter stringFromNumber:dic[@"packageDiscount"]];
        
        //智能缴费
        buttonView.titleLabel.text=[NSString stringWithFormat:@"%@个月",dic[@"packageCount"]];
        buttonView.detailLabel.text=[NSString stringWithFormat:@"%@折",string];
        buttonView.noteLabel.text=[NSString stringWithFormat:@"售出%@件",dic[@"salesVolume"]];
        buttonView.tagImageView.hidden=YES;
        [self.ui_borderView addSubview:buttonView];
    }
    self.ui_borderView.tag=9999;
}

- (void)setButtonSelectedWithTag:(NSInteger)tag
{
    for (XHFirstTypeTwoView *buttonView in self.ui_borderView.subviews) {
        [buttonView setSelected:NO];
    }
    XHFirstTypeTwoView *buttonView=[self.ui_borderView viewWithTag:tag];
    [buttonView setSelected:YES];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

#pragma mark - XHFirstTypeTwoViewDelegate
- (void)buttonViewDidSelectedAction:(XHFirstTypeTwoView *)sender{
    for (XHFirstTypeTwoView * buttonView in self.ui_borderView.subviews) {
        [buttonView setSelected:NO];
    }
    [sender setSelected:YES];
    if ([self.delegate respondsToSelector:@selector(cellDidSelectedData:index:)]) {
        NSDictionary *dic=self.m_dataArray[sender.tag];
        [self.delegate cellDidSelectedData:dic index:sender.tag];
    }
}

@end
